<template>
  <div>
    <el-dialog v-model="state.visible" append-to-body :close-on-click-modal="false" :before-close="hideModal"
      :width="state.width + 'mm'">
      <div id="preview_content" style="min-height: 100px" />
      <template #header>
        <div>
          <span class="mr20 fwb fz16">打印预览</span>
          <el-button :loading="state.waitShowPrinter" type="primary" icon="printer" @click.stop="print">
            打印
          </el-button>
          <el-button type="primary" icon="Printer" @click.stop="toPdf">
            pdf
          </el-button>
        </div>
      </template>
      <template #footer>
        <el-button key="close" type="info" @click="hideModal">关 闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    detail: {
      type: Object,
      default () {
        return {
          visible: false,
          spinning: true,
          waitShowPrinter: false,
          width: 0,// 纸张宽 mm
          hiprintTemplate: {},// 模板
          printData: {}// 数据
        };
      }
    },
    size: {
      type: String,
      default: 'small'
    },
    compile: {
      type: Boolean,
      default: false
    },
    second: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {

    };
  },
  watch: {

  },
  mounted () {
  },
  methods: {
    // 关闭弹窗
    hideModal () {
      this.visible = false;
    },
    // 显示预览
    showMadel (hiprintTemplate, printData, width = 180) {
      console.log(hiprintTemplate, printData, width);
      this.visible = true;
      this.spinning = true;
      this.width = Number(width);
      this.hiprintTemplate = hiprintTemplate;
      this.printData = printData;
      setTimeout(() => {
        // @ts-ignore
        $("#preview_content").html(hiprintTemplate.getHtml(this.printData));
        this.spinning = false;
      }, 500);
    },
    // 打印
    print () {
      this.waitShowPrinter = true;
      // @ts-ignore
      this.hiprintTemplate.print(
        this.printData,
        {},
        {
          callback: () => {
            console.log("callback");
            this.waitShowPrinter = false;
          }
        }
      );
    },
    // PDF
    toPdf () {
      console.log('printData---',this.printData)
      this.hiprintTemplate.toPdf(this.printData, "打印预览");
    }
  }
};
</script>

<style lang="scss" scoped>
:deep(.el-dialog__body) {
  padding: 0;
}
</style>
